package com.yao.ro.service.impl;


import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yao.common.exception.BusinessException;
import com.yao.ro.entity.dto.ExchangeDto;
import com.yao.ro.mapper.ExchangeMapper;
import com.yao.ro.service.ExchangeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Objects;

/**
* 交易所表 接口实现
*
* @author autoCode
* @version 1.0.0-SNAPSHOT
* @date 2020-04-09
*/
@Service
@Slf4j
public class ExchangeServiceImpl extends ServiceImpl<ExchangeMapper, ExchangeDto> implements ExchangeService{

    @Override
    public void add(ExchangeDto dto) {
        if (Objects.isNull(dto)) {
            log.warn("物品交易对象为空");
            return ;
        }

        ExchangeDto exchange = this.getById(dto.getId());

        if(Objects.isNull(exchange)){
            throw new BusinessException("根据id:{[]}查询不到商品交易信息", dto.getId());
        }

        exchange.setPrice(dto.getPrice());

        //  当前价格大于等于历史最大高价格
        if (dto.getPrice().compareTo(exchange.getHistoryMaxPrice()) >= 0) {
            exchange.setHistoryMaxPrice(dto.getPrice()).setSalable(1);
        }

        // 当前价格小于历史最低价格
        if(dto.getPrice().compareTo(exchange.getHistoryMinPrice()) < 0){
            exchange.setHistoryMinPrice(dto.getPrice()).setSalable(0);
        }

        JSONObject obj = JSONUtil.parseObj(exchange.getHistoryPrice());
        obj.put("time", LocalDateTime.now()).put("price", dto.getPrice());
        exchange.setHistoryPrice(JSONUtil.toJsonStr(obj));

        if (!this.updateById(exchange)) {
            throw new BusinessException("更新交易信息失败");
        }

    }

    @Override
    public ExchangeDto getByGoodsId(Integer id) {
        LambdaQueryWrapper<ExchangeDto> query = Wrappers.lambdaQuery();

        query.eq(ExchangeDto::getGoodsId, id);

        return this.getOne(query);
    }

    @Override
    public void updatePriceByGoodsId(Integer goodsId, BigDecimal price) {
        ExchangeDto exchange = this.getByGoodsId(goodsId);
        exchange.setPrice(price);

        // 当前价格大于等于历史最大高价格
        if (price.compareTo(exchange.getHistoryMaxPrice()) >= 0) {
            exchange.setHistoryMaxPrice(price).setSalable(1).setBuyable(0);
        }else{
            exchange.setSalable(0);
        }

        // 当前价格小于历史最低价格
        if (price.compareTo(exchange.getHistoryMinPrice()) <= 0) {
            exchange.setHistoryMinPrice(price).setSalable(0).setBuyable(1);
        }else{
            exchange.setBuyable(0);
        }


        String historyPrice = exchange.getHistoryPrice();
        JSONArray arr = JSONUtil.createArray();

        // 修复数据库存储为JSONObject的问题
        if (!StrUtil.startWith(historyPrice,"{")) {
            arr = JSONUtil.parseArray(historyPrice);
        }
        JSONObject obj = JSONUtil.createObj();
        obj.put("time", LocalDateTime.now()).put("price", price);
        arr.put(obj);

        exchange.setHistoryPrice(JSONUtil.toJsonStr(arr));

        exchange.setUpdateTime(null);

        if (!this.updateById(exchange)) {
            throw new BusinessException("更新交易信息失败");
        }
    }
}